from PyQt4 import QtCore, QtGui

from limber.ui.dialogs.ui_createnewdocument import Ui_CreateNewDocumentDialog
from limber.ui.dialogs import utils
from limber.ui import tools


class CreateNewDocumentDialog(QtGui.QDialog, Ui_CreateNewDocumentDialog):
    
    add_ability_request = QtCore.pyqtSignal(name='addAbilityRequest')
    details_request = QtCore.pyqtSignal((),(unicode,), name='detailsRequest')
    
    def __init__(self, parent=None,
                       document_names=(), view_names=()):
        super(CreateNewDocumentDialog, self).__init__(parent)
        
        self.setupUi(self)
        
        self.ok_button = self.button_box.button(QtGui.QDialogButtonBox.Ok)
        self.cancel_button = self.button_box.button(QtGui.QDialogButtonBox.Cancel)
        self.reset_button = self.button_box.button(QtGui.QDialogButtonBox.Reset)
        
        self.setupConnections()
        
        self.more_button.toggle()
        self.more_button.toggle()
        
        self.documents_combo.addItems(document_names)
        self.views_combo.addItems(view_names)
    
    def setupConnections(self):
        '''Sets up all the connections required.'''
        
        self.addability_button.clicked.connect(self.add_ability_request.emit)
        self.removeability_button.clicked.connect(self.remove_ability)
        self.details_button.clicked.connect(self.show_details)
        
        self.button_box.clicked[QtGui.QAbstractButton].connect(self.box_clicked)
    
    def box_clicked(self, button):
        '''Callback called when the button box is clicked.'''
        
        if button is self.ok_button or button is self.cancel_button:
            return
        
        self.reset_values()
    
    def add_ability(self, text):
        '''Adds an ability with the given *text*.'''
        
        self.ability_list.addItem(text)
    
    def remove_ability(self):
        '''Removes an ability.'''
        
        curitem = self.ability_list.currentItem()
        if not curitem:
            return
        
        self.ability_list.removeItemWidget(curitem)
    
    def show_details(self):
        '''Shows details.'''
        
        curitem = self.ability_list.currentItem()
        try:
            self.details_request[unicode].emit(curitem.text())
        except AttributeError:
            self.details_request.emit()
    
    def reset_values(self):
        '''Resets the values of the dialog.'''
        
        self.documents_combo.setCurrentIndex(0)
        self.views_combo.setCurrentIndex(0)
        
        self.ability_list.clear()
    
    def get_infos(self):
        '''Return a tuple with the right informations.'''
        
        document_name = unicode(self.documents_combo.currentText())
        view_name = unicode(self.views_combo.currentText())
        abilities = [unicode(el) for el in tools.widget_iterator(self.ability_list)]
        
        if not document_name:
            document_name = None
        
        if not view_name:
            view_name = None
        
        return (document_name, view_name, abilities)

utils.register_dialog(CreateNewDocumentDialog, 'create-new-document')